<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of villageAdminModel
 * 用户管理权限模型
 * @author xlp
 */
class villageAdminModel extends model {

    function __construct() {
        parent::__construct();
        $this->dbTable = 'village_admin';
    }

    /*
     * 检查用户是否是小区管理员，并判断相应模块是否有操作权限
     * @param $uid int UID
     * @param $vid int 小区id
     * @param $mod string 模块标记
     * @return bool
     */

    public function checkUserIsAllow($uid, $vid, $mod = '') {
        $userAllow = F('user/userAllow');
        if (!$userAllow) {
            $userAllow = $this->field('uid,village_id,allow_carpool,allow_forum,allow_sechand,allow_housekeep,allow_scenery')->where(array('status' => 1))->findAll();
            if ($userAllow) {
                $data = array();
                foreach ($userAllow as $val) {
                    $data[$val['village_id']][] = $val;
                }
                F('user/userAllow', $data);
            }
        }
        if (isset($userAllow[$vid])) {
            foreach ($userAllow[$vid] as $val) {
                if (isset($val['allow_' . $mod]) && $val['allow_' . $mod] && $val['uid'] == $uid) {
                    return true;
                }
            }
        }
        return false;
    }

    public function clearAllowCache() {
        F('user/userAllow', null);
        return true;
    }

}
